package com.znf.learn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.HashMap;
import java.util.Map;

public class DecryptEnvironmentPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String password = environment.getProperty("password");

        Map<String, Object> map = new HashMap<>();
        map.put("password","newpwd");

        // 创建一个新的MapPropertySource并给它命名
        PropertySource<?> propertySource = new MapPropertySource("myCustomSource", map);

        // 获取MutablePropertySources，添加新的PropertySource
        MutablePropertySources propertySources = environment.getPropertySources();
        propertySources.addLast(propertySource);

        // 打印出新设置的属性值
        System.out.println(environment.getProperty("password")); // 输出 "newValue"
    }
}
